home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / _OBSTACK.H < prev    next >
C/C++ Source or Header  |  1992-03-30  |  4KB  |  217 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _Obstack_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Obstack_h 1
  25.  
  26. #include <std.h>
  27.  
  28. class Obstack
  29. {
  30.   struct _obstack_chunk
  31.   {
  32.     char*           limit;
  33.     _obstack_chunk* prev;
  34.     char            contents[4];
  35.   };
  36.  
  37. protected:
  38.   long              chunksize;
  39.   _obstack_chunk* chunk;
  40.   char*              objectbase;
  41.   char*              nextfree;
  42.   char*              chunklimit;
  43.   int             alignmentmask;
  44.  
  45.   void  _free(void* obj);
  46.   void  newchunk(int size);
  47.  
  48. public:
  49.         Obstack(int size = 4080, int alignment = 4); // 4080=4096-mallocslop
  50.  
  51.         ~Obstack();
  52.  
  53.   void* base();
  54.   void* next_free();
  55.   int   alignment_mask();
  56.   int   chunk_size();
  57.   int   size();
  58.   int   room();
  59.   int   contains(void* p);      // does Obstack hold pointer p?
  60.  
  61.   void  grow(const void* data, int size);
  62.   void  grow(const void* data, int size, char terminator);
  63.   void  grow(const char* s);
  64.   void  grow(char c);
  65.   void  grow_fast(char c);
  66.   void  blank(int size);
  67.   void  blank_fast(int size);
  68.  
  69.   void* finish();
  70.   void* finish(char terminator);
  71.  
  72.   void* copy(const void* data, int size);
  73.   void* copy(const void* data, int size, char terminator);
  74.   void* copy(const char* s);
  75.   void* copy(char c);
  76.   void* alloc(int size);
  77.  
  78.   void  free(void* obj);
  79.   void  shrink(int size = 1); // suggested by ken@cs.rochester.edu
  80.  
  81.   int   OK();                   // rep invariant
  82. };
  83.  
  84.  
  85. inline Obstack::~Obstack()
  86. {
  87.   _free(0); 
  88. }
  89.  
  90. inline void* Obstack::base()
  91. {
  92.   return objectbase; 
  93. }
  94.  
  95. inline void* Obstack::next_free()
  96. {
  97.   return nextfree; 
  98. }
  99.  
  100. inline int Obstack::alignment_mask()
  101. {
  102.   return alignmentmask; 
  103. }
  104.  
  105. inline int Obstack::chunk_size()
  106. {
  107.   return chunksize; 
  108. }
  109.  
  110. inline int Obstack::size()
  111. {
  112.   return nextfree - objectbase; 
  113. }
  114.  
  115. inline int Obstack::room()
  116. {
  117.   return chunklimit - nextfree; 
  118. }
  119.  
  120. inline void Obstack:: grow(const void* data, int size)
  121. {
  122.   if (nextfree+size > chunklimit) 
  123.     newchunk(size);
  124.   bcopy(data, nextfree, size);
  125.   nextfree += size; 
  126. }
  127.  
  128. inline void Obstack:: grow(const void* data, int size, char terminator)
  129. {
  130.   if (nextfree+size+1 > chunklimit) 
  131.     newchunk(size+1);
  132.   bcopy(data, nextfree, size);
  133.   nextfree += size; 
  134.   *(nextfree)++ = terminator; 
  135. }
  136.  
  137. inline void Obstack:: grow(const char* s)
  138. {
  139.   grow((void*)s, strlen(s), 0); 
  140. }
  141.  
  142. inline void Obstack:: grow(char c)
  143. {
  144.   if (nextfree+1 > chunklimit) 
  145.     newchunk(1); 
  146.   *(nextfree)++ = c; 
  147. }
  148.  
  149. inline void Obstack:: blank(int size)
  150. {
  151.   if (nextfree+size > chunklimit) 
  152.     newchunk(size);
  153.   nextfree += size; 
  154. }
  155.  
  156. inline void* Obstack::finish(char terminator)
  157. {
  158.   grow(terminator); 
  159.   return finish(); 
  160. }
  161.  
  162. inline void* Obstack::copy(const void* data, int size)
  163. {
  164.   grow (data, size);
  165.   return finish(); 
  166. }
  167.  
  168. inline void* Obstack::copy(const void* data, int size, char terminator)
  169. {
  170.   grow(data, size, terminator); 
  171.   return finish(); 
  172. }
  173.  
  174. inline void* Obstack::copy(const char* s)
  175. {
  176.   grow((void*)s, strlen(s), 0); 
  177.   return finish(); 
  178. }
  179.  
  180. inline void* Obstack::copy(char c)
  181. {
  182.   grow(c);
  183.   return finish(); 
  184. }
  185.  
  186. inline void* Obstack::alloc(int size)
  187. {
  188.   blank(size);
  189.   return finish(); 
  190. }
  191.  
  192. inline void Obstack:: free(void* obj)     
  193. {
  194.   if (obj >= (void*)chunk && obj<(void*)chunklimit)
  195.     nextfree = objectbase = (char *) obj;
  196.   else 
  197.     _free(obj); 
  198. }
  199.  
  200. inline void Obstack:: grow_fast(char c)
  201. {
  202.   *(nextfree)++ = c; 
  203. }
  204.  
  205. inline void Obstack:: blank_fast(int size)
  206. {
  207.   nextfree += size; 
  208. }
  209.  
  210. inline void Obstack:: shrink(int size) // from ken@cs.rochester.edu
  211. {
  212.   if (nextfree >= objectbase + size)
  213.     nextfree -= size;
  214. }
  215.  
  216. #endif
  217.